package com.zhongyu.config;

import com.sun.org.apache.regexp.internal.RE;
import com.zhongyu.service.PermissionService;
import com.zhongyu.service.UserService;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
       @Resource
     private UserService userService;
       @Resource
       private PermissionService permissionService;
    /*  @Resource
      private  SmbmsRealm smbmsRealm;*/
    @Bean
    public SecurityManager securityManager(){
        DefaultWebSecurityManager securityManager=new DefaultWebSecurityManager();
        securityManager.setRealm(smbmsRealm());
        securityManager.setCacheManager(ehCacheManager());
        return  securityManager;
    }

    /**
     * 入口类
     * @return
     */
    @Bean
public ShiroFilterFactoryBean shiroFilterFactoryBean(){
    ShiroFilterFactoryBean shiroFilterFactoryBean=new ShiroFilterFactoryBean();
   shiroFilterFactoryBean.setSecurityManager(securityManager());
        Map<String,String>filterChains=new HashMap<>();
   shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChains);
        filterChains.put("/logout","logout");//默认会退出会跳转到"/"这个路径
        filterChains.put("/login","anon");
        filterChains.put("/css/**","anon");
        filterChains.put("/attach/**","anon");
        filterChains.put("/comm/**","anon");
        filterChains.put("/js/**","anon");
        filterChains.put("/webjars/**","anon");
        filterChains.put("/**","authc");
   shiroFilterFactoryBean.setLoginUrl("/login");
   return  shiroFilterFactoryBean;
}
    @Bean
public  SmbmsRealm smbmsRealm(){
        SmbmsRealm smbmsRealm=new SmbmsRealm();
  smbmsRealm.setCredentialsMatcher(credentialsMatcher());
  smbmsRealm.setUserService(userService);
  smbmsRealm.setPermissionService(permissionService);
        return  smbmsRealm;
}
    @Bean
public HashedCredentialsMatcher credentialsMatcher(){
        HashedCredentialsMatcher matcher=new HashedCredentialsMatcher();
matcher.setHashAlgorithmName("md5");
matcher.setHashIterations(1);
        return  matcher;
}

    /**
     * 开启shiro注解
     * @return
     */
    @Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(){
        AuthorizationAttributeSourceAdvisor advisor=new AuthorizationAttributeSourceAdvisor();
      advisor.setSecurityManager(securityManager());
        return  advisor;
}

     @Bean
    public EhCacheManager ehCacheManager(){
        EhCacheManager ehCacheManager=new EhCacheManager();
  ehCacheManager.setCacheManagerConfigFile("classpath:shiro-ehcache.xml");
    return  ehCacheManager;
}
}
